<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Verify | Craft and talk</title>
<?php
include 'head.php';
?>
</head>
<body>
<?php
include 'nav.php';
?>
<div id="wrapper">
<div id="content">
<h1>Verify unsubscription</h1>
<?php

if(isSet($_GET['code']) && isSet($_GET['email'])){
	verifySubscriber(urldecode($_GET['code']), $_GET['email']);
	
} elseif (isSet($_GET['email'])) {
	$box = "<h2>Verify subscription</h2><form method = 'get' action=''>Verification code: <input type='hidden' name='email' value='".$_GET['email']."'></input><br /><input type='text' name='code'></input><br /><input type='submit' value='Verify'></input></form>";
	echo $box;
} else {
    echo "Not found";
}

function verifySubscriber($code, $mail){
	include 'connect.php';
	mysql_select_db($db, $con);
	$table = 'subscribers';
		
	$query  = "SELECT * FROM {$table} WHERE email = '{$mail}'";
	
	$result = mysql_query($query) or die(mysql_error());
	
	if(mysql_num_rows($result) == 1){
		$row = mysql_fetch_row($result);
		if($row[2] == $code){
			$query  = "DELETE FROM {$table} WHERE email = '{$mail}'";
			$result = mysql_query($query) or die(mysql_error());
			echo "<p>You are unsubscribed from CraftnTalk. Thanks for the interest</p>";
		}else{
			echo "Invalid code.";
			die();
		}
	} else {
		echo "You haven't requested for a subscription yet. Click <a href='subscribe.php'>here</a> to goto the subscription page.";
	}
	mysql_close($con);
}
?>
</div>
</div>
<?php
include 'footer.php';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="js/jQuery.Validate.min.js"></script>
</body>
</html>